package com.gouaixin.bee.beeframework2.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.bean.Goods_id;
import com.gouaixin.bee.beeframework2.bean.Pay;
import com.gouaixin.bee.beeframework2.extra.myutils.YHttpUtil;
import com.gouaixin.bee.beeframework2.model.MemberModel;
import com.gouaixin.bee.beeframework2.tool.FileUtils;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;

/**
 * Created by Administrator on 2015/3/10.
 */
//缘分红娘（修改后的）
public class B16_yuanfenHongniangyueActivity_new extends FragmentActivity {
    Context context = B16_yuanfenHongniangyueActivity_new.this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b16_yuanfenhongniang_new);
        cid= PreferencesUtils.getString(context, "cid");

        initHead();
        initContent();
    }

    WebView contentWebView01;
    WebView contentWebView02;
    WebView contentWebView03;
//    WebView contentWebView04;

    private String order_name="",order_price="";
    private int goods_id;//商品的唯一id
    Pay bean=new Pay();
    String cid="";
    Intent it;

    /**
     * 内容区域的监听
     */
    private void initContent() {
        TextView kt01 = (TextView) findViewById(R.id.kt01);
        TextView kt02 = (TextView) findViewById(R.id.kt02);
        TextView kt03 = (TextView) findViewById(R.id.kt03);
//        TextView mtv01= (TextView) findViewById(R.id.mtv01);
        contentWebView01 = (WebView) findViewById(R.id.webview01);
        contentWebView02 = (WebView) findViewById(R.id.webview02);
        contentWebView03 = (WebView) findViewById(R.id.webview03);


//        contentWebView04 = (WebView) findViewById(R.id.webview04);
//        contentWebView04.loadUrl("http://avatar.csdn.net/9/A/6/1_beyond0525.jpg");


        //根据是否充值过，判断显示的充值项目
        MemberModel.haveRecharge(context, new MemberModel.HaveRechargeListener() {
            @Override
            public void onResult(boolean haveRecharge) {
                //如果充值过
                if (haveRecharge) {
                    contentWebView01.setVisibility(View.GONE);
                    contentWebView02.setVisibility(View.GONE);
                }
                //如果没充值过
                else {
                    contentWebView01.setVisibility(View.VISIBLE);
                    contentWebView02.setVisibility(View.VISIBLE);
                }
            }
        });


//        WebView webview_baidu = (WebView) findViewById(R.id.webview_baidu);
        int color = getResources().getColor(R.color.bantouming);
        contentWebView01.setBackgroundColor(color); // 设置背景色
        contentWebView02.setBackgroundColor(color); // 设置背景色
        contentWebView03.setBackgroundColor(color); // 设置背景色
//        webview_baidu.setBackgroundColor(color); // 设置背景色


//        mtv01.setText(Html.fromHtml("<font color=\"#a3a3a3\">" + "可以通过" + "</font><font color=\"#FF0541\">" + "年龄、身高、地区、星座、收入" + "</font><font color=\"#a3a3a3\">" + "等多个条件来找到你喜欢的MM、GG" + "</font>"));

        kt01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                MemberModel.hongniangSuccess(context, HongniangType.Type_100);
                order_name="人工红娘";
                order_price="100";

                bean.setAmount(order_price);
                bean.setCid(cid);
                bean.setTradeName(order_name);
                bean.setGoods_id(Goods_id.Hongniang_100);

                it=new Intent(context,F1_PaySelectActivity.class);
                it.putExtra("pay",new Gson().toJson(bean));
                mLog.e("vip界面传出的pay=" + new Gson().toJson(bean));
                startActivity(it);

            }
        });

        kt02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                MemberModel.hongniangSuccess(context, HongniangType.Type_200);
                order_name="人工红娘";
                order_price="200";

                bean.setAmount(order_price);
                bean.setCid(cid);
                bean.setTradeName(order_name);
                bean.setGoods_id(Goods_id.Hongniang_200);

                it=new Intent(context,F1_PaySelectActivity.class);
                it.putExtra("pay",new Gson().toJson(bean));
                mLog.e("vip界面传出的pay=" + new Gson().toJson(bean));
                startActivity(it);

            }
        });

        kt03.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                MemberModel.hongniangSuccess(context, HongniangType.Type_300);
                order_name="人工红娘";
                order_price="300";

                bean.setAmount(order_price);
                bean.setCid(cid);
                bean.setTradeName(order_name);
                bean.setGoods_id(Goods_id.Hongniang_300);

                it=new Intent(context,F1_PaySelectActivity.class);
                it.putExtra("pay",new Gson().toJson(bean));
                mLog.e("vip界面传出的pay=" + new Gson().toJson(bean));
                startActivity(it);
            }
        });

        String file_name01 = "huodong01.html";
        String file_name02 = "hongniang.html";
        String file_name03 = "huodong03.html";


//        HttpUtil.loadAssetsHtml(context, contentWebView01, file_name01, "demo");
        String html01 = FileUtils.readAssest(this, "html/hongniang.html");
        html01 = replaceHtmlString(html01, 0);
        YHttpUtil.loadAssetsHtmlString(context, contentWebView01, html01, "demo");

        String html02 = FileUtils.readAssest(this, "html/hongniang.html");
        html02 = replaceHtmlString(html02, 1);
        YHttpUtil.loadAssetsHtmlString(context, contentWebView02, html02, "demo");

        String html03 = FileUtils.readAssest(this, "html/hongniang.html");
        html03 = replaceHtmlString(html03, 2);
        YHttpUtil.loadAssetsHtmlString(context, contentWebView03, html03, "demo");

    }

    String[] price = {"100元/年", "200元/年", "300元/年"};
    String[] number = {"1~3名", "6~10名", "6~10名顶级白领校花模特"};
    String[] instruction = {"同城适龄异性见面约会", "同城适龄异性见面约会", "见面约会"};
    String[] icon = {"hn_01.png", "hn_02.png", "hn_03.png"};

    /**
     * 替换Html中的标记符
     */
    private String replaceHtmlString(String html, int flag) {
        html = html.replace("@price", price[flag]);
        html = html.replace("@number", number[flag]);
        html = html.replace("@instruction", instruction[flag]);
        html = html.replace("@icon", icon[flag]);
        html = html.replace("@flag", flag + "");

//        html = html.replace("#", "%23");
//        html = html.replace("%", "%25");
//        html = html.replace("\\", "%27");
//        html = html.replace("?", "%3f");
        return html;
    }

    /**
     * 头部的监听
     */
    private void initHead() {
        //顶部
        ImageView back;//返回
        TextView title;//标题

        back = (ImageView) findViewById(R.id.back);
        title = (TextView) findViewById(R.id.title);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @JavascriptInterface
    public void clickOnAndroid() {
        Log.e("Demo", "js调用了java函数");
//        Toast.makeText(this, "js调用了java函数", Toast.LENGTH_SHORT).show();
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
//                msgView.setText(msgView.getText() + "\njs调用了java函数");

            }
        });
    }


    @JavascriptInterface
    public void clickOnAndroid(final int flag) {
        Log.e("Demo", "js调用了java函数:flag=" + flag);
//        Toast.makeText(this, "js调用了java函数:flag=" + flag, Toast.LENGTH_SHORT).show();
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
//                msgView.setText(msgView.getText() + "\njs调用了java函数");
                setListenerInJs(flag);

            }
        });
    }

    /**
     * 实现js接口的点击事件
     *
     * @param flag
     */
    private void setListenerInJs(int flag) {
        switch (flag) {
            case 0:
//                MemberModel.hongniangSuccess(context, HongniangType.Type_100);
                order_name="人工红娘";
                order_price="100";

                bean.setAmount(order_price);
                bean.setCid(cid);
                bean.setTradeName(order_name);
                bean.setGoods_id(Goods_id.Hongniang_100);

                it=new Intent(context,F1_PaySelectActivity.class);
                it.putExtra("pay",new Gson().toJson(bean));
                mLog.e("vip界面传出的pay=" + new Gson().toJson(bean));
                startActivity(it);
                break;
            case 1:
//                MemberModel.hongniangSuccess(context, HongniangType.Type_200);
                order_name="人工红娘";
                order_price="200";

                bean.setAmount(order_price);
                bean.setCid(cid);
                bean.setTradeName(order_name);
                bean.setGoods_id(Goods_id.Hongniang_200);

                it=new Intent(context,F1_PaySelectActivity.class);
                it.putExtra("pay",new Gson().toJson(bean));
                mLog.e("vip界面传出的pay=" + new Gson().toJson(bean));
                startActivity(it);
                break;
            case 2:
//                MemberModel.hongniangSuccess(context, HongniangType.Type_300);
                order_name="人工红娘";
                order_price="300";

                bean.setAmount(order_price);
                bean.setCid(cid);
                bean.setTradeName(order_name);
                bean.setGoods_id(Goods_id.Hongniang_300);

                it=new Intent(context,F1_PaySelectActivity.class);
                it.putExtra("pay",new Gson().toJson(bean));
                mLog.e("vip界面传出的pay=" + new Gson().toJson(bean));
                startActivity(it);
                break;
        }


    }
}
